package com.CTAtracker.android.Adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Models.AroundStation;

/**
 * This class is a adapter for the Station around the user. 
 */
public class StationsAroundItemAdapter extends ArrayAdapter<AroundStation> {
	/**
	 * The layout to inflate 
	 */
	private LayoutInflater _layoutInflater = null;
	/**
	 * The id of the resource 
	 */
	private int _resourceId = 0;
	/**
	 * The array list to adapt to the view
	 */
	private ArrayList<AroundStation> _listStations;
	/**
	 * The current position of the user
	 */
	
	/**
	 * Constructor of the class. Instance the attribute and call the super method.
	 * @param context The context of the application
	 * @param resourceId The id of the resource to inflate 
	 * @param objects the list of station around to adapt to the view
	 */
	public StationsAroundItemAdapter(Context context, int resourceId, List<AroundStation> objects) {
		super(context, resourceId, 0, objects);

		_layoutInflater = LayoutInflater.from(context);
		_resourceId = resourceId;
		_listStations = (ArrayList<AroundStation>) objects;
	}

	/**
	 * This method is call every time we want to display the view
	 * Display the station of the list with the distance to the user
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final AroundStation aroundStation = _listStations.get(position);
		convertView = _layoutInflater.inflate(_resourceId, parent, false);

		TextView name = (TextView) convertView.findViewById(R.id.textViewName);
		TextView distance = (TextView) convertView.findViewById(R.id.textViewDistance);
		LinearLayout linearLayout = (LinearLayout) convertView.findViewById(R.id.aroundLinearLayoutColor);
		
		name.setText(aroundStation.get_name());
		distance.setText(aroundStation.distanceToString());
		
		for(int i=0; i< aroundStation.getListColorStation().size(); i++){
			FrameLayout f = new FrameLayout(convertView.getContext());
			f.setLayoutParams(
					new android.widget.LinearLayout.LayoutParams(
							android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 
							60/aroundStation.getListColorStation().size()));
			
			f.setBackgroundColor(Color.parseColor(aroundStation.getListColorStation().get(i).toString()));
			linearLayout.addView(f,0);
		}
		
		return convertView;
	}
}
